home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / vmmngr.zip / EXDYNARR.PAS < prev    next >
Pascal/Delphi Source File  |  1990-07-16  |  5KB  |  165 lines

  1. program  DynArrayExample;
  2.  
  3. uses
  4.   OpCrt,
  5.   OpRoot,
  6.   Vmmngr;
  7.  
  8. type
  9.   ExpRecord
  10.     = record
  11.        Name : string[25];
  12.        Age,
  13.        Class: word;
  14.       end;
  15. var
  16.   ExpArray  : DynArray;
  17.   RecBuffer : ExpRecord; {buffer to put and get data to and from ExpArray}
  18.   Key,
  19.   Status,
  20.   NbElem,
  21.   NumIncr   : word;
  22.   S         : DosIdStream;
  23.  
  24.   procedure ShowStatus;
  25.     {-Display status of ExpArray}
  26.   begin
  27.     Writeln('ExpArray size is now : ', ExpArray.GetArraySize);
  28.     Writeln('ExpArray status is   : ', ExpArray.GetStatus);
  29.   end;
  30.  
  31.   procedure ShowContents(index : word);
  32.     {-Show contents of one element of ExpArray}
  33.   begin
  34.    ExpArray.GetElem(index, RecBuffer);
  35.    Writeln('Element #',index, ' Name  is : ', RecBuffer.Name);
  36.    Writeln('Element #',index, ' Age   is : ', RecBuffer.Age);
  37.    Writeln('Element #',index, ' Class is : ', RecBuffer.Class);
  38.   end;
  39.  
  40. begin
  41.   Write('Enter the maximum number of elements for the array : ');
  42.   ReadLn(NbElem);
  43.   Write('Enter the minimum increment when growing (number of elements) : ');
  44.   ReadLn(NumIncr);
  45.   ClrScr;
  46.   {Creates a dynamic array of ExpRecord}
  47.   if not ExpArray.Init(NbElem,
  48.                        SizeOf(ExpRecord),
  49.                        NumIncr) then begin
  50.     WriteLn('Failed to Create DynArray,  Status = ', InitStatus);
  51.     Halt;
  52.   end;
  53.   Writeln('After initialization, the array size is 0...');
  54.   ShowStatus;
  55.  
  56.   {Load RecBuffer}
  57.   Recbuffer.Name  := 'Name0';
  58.   Recbuffer.Age   := 0; {Not realistic but helps seeing how things work}
  59.   RecBuffer.Class := 0;
  60.  
  61.   {Store RecBuffer in the dynamic array}
  62.   ExpArray.SetElem(0, RecBuffer);
  63.  
  64.   {Size increase to the minimum increment}
  65.   Writeln('Setting the first element will increase the size to the minimum increment...');
  66.   Writeln('One element occupies ', ExpArray.GetElemSize, ' bytes');
  67.   ShowStatus;
  68.   ShowContents(0);
  69.  
  70.   {Load another value at maximum index }
  71.   Recbuffer.Name  := 'NameMax';
  72.   Recbuffer.Age   := ExpArray.GetMaxIndex;
  73.   RecBuffer.Class := Recbuffer.Age;
  74.   ExpArray.SetElem(ExpArray.GetMaxIndex, RecBuffer);
  75.  
  76.   {The size was increased to receive the last element}
  77.   Writeln('Setting the last element will likely require a size increase...');
  78.   ShowStatus;
  79.   ShowContents(ExpArray.GetMaxIndex);
  80.  
  81.   {Elements non explicitly set are always 0}
  82.   Writeln('You can always assume that an element which has not been set is null...');
  83.   ShowContents(ExpArray.GetMaxIndex-1);
  84.   Writeln('Press any key');
  85.   While not KeyPressed do;
  86.   While Keypressed do Key :=ReadKeyWord;
  87.   ClrScr;
  88.  
  89.   {Now storing the array in a stream, killing it and reloading it}
  90.   Writeln('Now storing the array...');
  91.   {Create a Stream, check for success}
  92.   if not S.Init('DYNARRAY.STM', SCreate) then begin
  93.     Writeln('Failed to Create Stream,  Status = ', InitStatus);
  94.     Halt;
  95.   end;
  96.   {Register object hierarchy}
  97.   S.RegisterHier(DynArrayStream);
  98.   Status := S.GetStatus;
  99.   if Status <> 0 then begin
  100.     Writeln('Failed to Register objects,  Status = ', Status);
  101.     Halt;
  102.   end;
  103.   S.Put(ExpArray);
  104.   Status := S.GetStatus;
  105.   if Status <> 0 then begin
  106.     WriteLn('Failed to Write ExpArray,   Status = ', Status);
  107.     Halt;
  108.   end;
  109.  
  110.   Writeln('Now killing ExpArray...');
  111.   ExpArray.Done;
  112.   S.done;
  113.  
  114.   Writeln('Now reloading ExpArray...');
  115.   {Re-open the existing Stream}
  116.   if not S.Init('DYNARRAY.STM', SOpen) then begin
  117.     Writeln('Failed to Open Stream,  Status = ', InitStatus);
  118.     Halt;
  119.   end;
  120.   {Re-register object hierarchy}
  121.   S.RegisterHier(DynArrayStream);
  122.   Status := S.GetStatus;
  123.   if Status <> 0 then begin
  124.     Writeln('Failed to register objects. Status = ', Status);
  125.     Halt;
  126.   end;
  127.   S.Get(ExpArray);
  128.   Status := S.GetStatus;
  129.   if Status <> 0 then begin
  130.     Writeln ('Failed to read ExpArray. Status = ', Status);
  131.     Halt;
  132.   end;
  133.   S.Done;
  134.   Writeln('Showing ExpArray first and last elements...');
  135.   ShowStatus;
  136.   ShowContents(ExpArray.GetMaxIndex);
  137.   ShowContents(0);
  138.   Writeln;
  139.   Writeln('Press any key');
  140.   While not KeyPressed do;
  141.   While Keypressed do Key :=ReadKeyWord;
  142.   ClrScr;
  143.  
  144.   {Clearing the array just resets the size to 0 and free all data on the heap}
  145.   {However the array is still active and any element could be set}
  146.   Writeln('Now clearing the array...');
  147.   ExpArray.Clear;
  148.   Writeln('ExpArray size is now ', ExpArray.GetArraySize, ' bytes');
  149.   Writeln('ExpArray still alive : any element can be set.');
  150.  
  151.   Writeln('Now testing error checking...');
  152.   Writeln('Trying to get the second element of the cleared array...');
  153.   ExpArray.GetElem(2, RecBuffer);
  154.   if ExpArray.GetStatus <> 0 then
  155.      Writeln('Failed to retrieve element data');
  156.   Writeln;
  157.   Writeln('Now trying to setup a very big array (30000 elements)...');
  158.   ExpArray.Done;
  159.   ExpArray.Init(30000, SizeOf(ExpRecord), 100);
  160.   if InitStatus <> 0 then
  161.     WriteLn('Failed to Create DynArray,  Status = ', InitStatus);
  162.  
  163.   ExpArray.done;
  164. end.
  165.